package xio.ccf_201803;

import java.util.*;
import java.util.regex.*;

/**
 * ccf 201803-3 100分
 * url映射
 */
public class UrlMap {
    public static void main(String... args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        Map<Pattern,String> map = new HashMap<>(m);
        for (int i = 0; i < m; i++) {
            String src = sc.next();
            String value = sc.next(); //规则名
            //根据每个规则生成正则表达式
            src = src.replaceAll("<str>","([^/]+)")
            .replaceAll("<int>","(\\\\d+)")
            .replaceAll("<path>","(\\\\S+)")
            .replaceAll("\\.","\\\\.")  // 减号 -、下划线 _ 和小数点 .在正则里有特殊含义，需要转义成普通字符
            .replaceAll("\\-","\\\\-")
            .replaceAll("\\_","\\\\_");
            src = "^"+src+"$";
            map.put(Pattern.compile(src),value);
        }
        label:
        for (int i = 0; i < n; i++) {
            String test = sc.next(); //待匹配url
            Matcher matcher;
            for (Pattern p : map.keySet()) {
                matcher = p.matcher(test);
                StringBuilder sb = new StringBuilder();
                if (matcher.find()){ //匹配成功
                    sb.append(map.get(p));
                    for (int j = 1; j <= matcher.groupCount(); j++) {
                        sb.append(" ").append(removePrefixZero(matcher.group(j)));
                    }
                    System.out.println(sb.toString());
                    continue label; //匹配成功直接下一个test
                }
            }
            //匹配失败
            System.out.println("404");
        }
    }

    /**
     * 移除数字的前导0
     */
    private static String removePrefixZero(String str){
        Matcher m = Pattern.compile("^[0]+(\\d+)").matcher(str);
        if (m.find()){
            str = m.group(1);
        }
        return str;
    }
}
